package com.genghis.ptas.statistic;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.classify.entity.Type;
import com.genghis.ptas.classify.entity.TypeValues;
import com.genghis.ptas.classify.service.ClassifyService;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.course.entity.CourseInfo;
import com.genghis.ptas.course.service.CourseStatisticsService;
import com.genghis.ptas.statistic.entity.CourseStatistics;
import com.genghis.ptas.statistic.entity.Range;
import com.genghis.ptas.statistic.entity.Statistic;
import com.genghis.ptas.statistic.entity.TypeStatistic;
import com.genghis.ptas.statistic.service.StatisticService;
import com.genghis.ptas.system.entity.DictValues;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/statistic/")
public class CourseStatisticController {
    @Autowired
    private StatisticService statisticService;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private DictService dictService;

    @Autowired
    private CourseStatisticsService courseStatisticsService;

    @RequestMapping(value = "statisticType", method = {RequestMethod.GET, RequestMethod.POST})
    public String statisticTypeDo(HttpServletRequest request) {
        List<Type> typeList = classifyService.findAllType();
        request.setAttribute("typeList", typeList);
        return "backstage/statistic/statisticTypeResultDo";
    }

    @RequestMapping(value = "statisticCourse", method = {RequestMethod.GET, RequestMethod.POST})
    public String statisticCourse(HttpServletRequest request) {
        String typeId = request.getParameter("typeName") == null ? "" : classifyService.changeTypeValueToCode(StringUtil.iso2utf(request.getParameter("typeName"))).toString();
        request.setAttribute("typeId", typeId);
        return "backstage/statistic/listAllStatistic";
    }

    @RequestMapping(value = "listAllUserStatisticDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String listAllUserStatisticDo() {
        return "backstage/statistic/listAllUserStatisticDo";
    }

    @RequestMapping(value = "listAllUserStatistic", method = {RequestMethod.POST})
    @ResponseBody
    public Page<Statistic> listAllUserStatistic(PageParameter pageParameter, Statistic statistic, HttpServletRequest request) {
        String collectLowerNumber = request.getParameter("collectLowerNumber");
        String collectUpperNumber = request.getParameter("collectUpperNumber");
        String registerLowerNumber = request.getParameter("registerLowerNumber");
        String registerUpperNumber = request.getParameter("registerUpperNumber");
        Range range = new Range(collectLowerNumber, collectUpperNumber, registerLowerNumber, registerUpperNumber, "");
        return courseStatisticsService.findUserStatisticList(pageParameter, statistic, range);
    }

    @RequestMapping(value = "listAllStatisticDo", method = {RequestMethod.POST})
    @ResponseBody
    public Page<CourseStatistics> listAllStatisticDo(PageParameter pageParameter, CourseStatistics courseStatistics, HttpServletRequest request) {
        String collectLowerNumber = request.getParameter("collectLowerNumber");
        String collectUpperNumber = request.getParameter("collectUpperNumber");
        String registerLowerNumber = request.getParameter("registerLowerNumber");
        String registerUpperNumber = request.getParameter("registerUpperNumber");
        String qualityId = request.getParameter("qualityId");
        Range range = new Range(collectLowerNumber, collectUpperNumber, registerLowerNumber, registerUpperNumber, qualityId);
        return courseStatisticsService.findAllStatisticList(pageParameter, courseStatistics, range);
    }

    @RequestMapping(value = "statisticRegister", method = {RequestMethod.GET, RequestMethod.POST})
    public String statisticCourseDo(HttpServletRequest request, @RequestParam("typeName") String typeName) {
        request.setAttribute("typeName", StringUtil.iso2utf(typeName));
        List<TypeValues> typeValuesList = classifyService.findAllTypeValue();
        List<DictValues> statistic_items = dictService.findAllDictValues(DictConstant.STATISTIC_ITEMS);
        List<DictValues> statistic_style = dictService.findAllDictValues(DictConstant.STATISTIC_STYLE);
        List<DictValues> ranking = dictService.findAllDictValues(DictConstant.RANKING);
        request.setAttribute("typeList", typeValuesList);
        request.setAttribute("statistic_style", statistic_style);
        request.setAttribute("statistic_items", statistic_items);
        request.setAttribute("ranking", ranking);
        return "backstage/statistic/statisticCourseResult";
    }

    @RequestMapping(value = "statisticRegisterType", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<TypeStatistic> statisticRegisterType(@RequestParam("ids") String ids) {
        return statisticService.statisticRegisterType(ids);
    }

    @RequestMapping(value = "statisticAllType", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<TypeStatistic> statisticAllType(@RequestParam("ids") String ids) {
        return statisticService.statisticTotalSources(ids);
    }

    @RequestMapping(value = "statisticCourseDo", method = {RequestMethod.POST})
    @ResponseBody
    public List<CourseStatistics> getRegisterCourseData(CourseStatistics courseStatistic) {
        return statisticService.statisticCourse(courseStatistic);
    }

    @RequestMapping(value = "assessQualityDo", method = {RequestMethod.GET})
    public String assessQualityDo(HttpServletRequest request) {
        String courseId = request.getParameter("id");
        request.setAttribute("courseId", courseId);
        return "backstage/statistic/assessQualityDo";
    }

    @RequestMapping(value = "detailStatistic ", method = {RequestMethod.GET})
    public String detailStatistic(HttpServletRequest request) {
        String typeName = request.getParameter("typeName");
        List<TypeStatistic> detailStatistic = statisticService.statisticDetail(typeName);
        request.setAttribute("typeName", typeName);
        request.setAttribute("detailStatistic", detailStatistic);
        request.setAttribute("typeLen", detailStatistic.size());
        return "backstage/statistic/detailStatistic";
    }

    @RequestMapping(value = "detailResource ", method = {RequestMethod.GET})
    public String detailResource(HttpServletRequest request) {
        String typeName = request.getParameter("typeName");
        List<TypeStatistic> detailStatistic = statisticService.detailResource(typeName);
        request.setAttribute("typeName", typeName);
        request.setAttribute("detailStatistic", detailStatistic);
        request.setAttribute("typeLen", detailStatistic.size());
        return "backstage/statistic/detailResource";
    }

    @RequestMapping(value = "pieAnalysisDo ", method = {RequestMethod.GET})
    public String pieAnalysisDo(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        List<CourseStatistics> pieAnalysis = statisticService.analysisCourse(ids);
        request.setAttribute("pieAnalysis", pieAnalysis);
        if (request.getParameter("times") == null) {
            request.setAttribute("times", 1);
        } else {
            request.setAttribute("times", Integer.parseInt(request.getParameter("times") + 1));
        }
        return "backstage/statistic/pieAnalysisCourseDo";
    }

    @RequestMapping(value = "columnAnalysisDo ", method = {RequestMethod.GET})
    public String columnAnalysisDo(HttpServletRequest request) {
        request.setAttribute("ids", request.getParameter("ids"));
        if (request.getParameter("times") == null) {
            request.setAttribute("times", 1);
        } else {
            request.setAttribute("times", Integer.parseInt(request.getParameter("times") + 1));
        }
        return "backstage/statistic/columnAnalysisCourseDo";
    }

    @RequestMapping(value = "columnAnalysis", method = {RequestMethod.POST})
    @ResponseBody
    public List<CourseStatistics> columnAnalysis(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        return statisticService.analysisCourse(ids);
    }

    @RequestMapping(value = "pieAnalysisUserDo ", method = {RequestMethod.GET})
    public String pieAnalysisUserDo(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        List<Statistic> pieAnalysis = statisticService.analysisUser(ids);
        request.setAttribute("pieAnalysis", pieAnalysis);
        return "backstage/statistic/pieAnalysisUserDo";
    }

    @RequestMapping(value = "columnAnalysisUserDo ", method = {RequestMethod.GET})
    public String columnAnalysisUserDo(HttpServletRequest request) {
        request.setAttribute("ids", request.getParameter("ids"));
        return "backstage/statistic/columnAnalysisUserDo";
    }

    @RequestMapping(value = "columnAnalysisUser", method = {RequestMethod.POST})
    @ResponseBody
    public List<Statistic> columnAnalysisUser(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        return statisticService.analysisUser(ids);
    }

    @RequestMapping(value = "assessQuality", method = {RequestMethod.POST})
    @ResponseBody
    public void assessQuality(CourseInfo courseInfo) {
        courseStatisticsService.assessQuality(courseInfo);
    }

    @RequestMapping(value = "pieAnalysisUserLikeDo ", method = {RequestMethod.GET})
    public String pieAnalysisUserLikeDo(HttpServletRequest request) {
        String userId = request.getParameter("ids");
        List<TypeStatistic> userAnalysis = statisticService.analysisUserLike(userId);
        request.setAttribute("userAnalysis", userAnalysis);
        return "backstage/statistic/pieAnalysisUserLikeDo";
    }

    @RequestMapping(value = "columnAnalysisUserLikeDo ", method = {RequestMethod.GET})
    public String columnAnalysisUserLikeDo(HttpServletRequest request) {
        request.setAttribute("ids", request.getParameter("ids"));
        return "backstage/statistic/columnAnalysisUserLikeDo";
    }

    @RequestMapping(value = "columnAnalysisUserLike", method = {RequestMethod.POST})
    @ResponseBody
    public List<TypeStatistic> columnAnalysisUserLike(HttpServletRequest request) {
        String userId = request.getParameter("ids");
        return statisticService.analysisUserLike(userId);
    }
}
